package com.njtcedu.microblog.common.repository;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.njtcedu.microblog.common.exception.BusinessException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CompressImage {
    
    private static CompressImage instance;

    private  CompressImage() { }
    
    public static synchronized CompressImage getInstance() {
        if (null == instance) {
            instance = new CompressImage();
        }
        return instance;
    }

    public Integer[] compress(String sourcePath, String destPath, Integer width, Integer height) throws IOException {
        OutputStream destStream = null; 
        InputStream inStream = null;
        Image img = new ImageIcon(sourcePath).getImage();
        
        Integer sourceWidth = img.getWidth(null);
        Integer sourceHeight = img.getHeight(null);
        
        double rate1 =  (width * 1.0) / (sourceWidth * 1.0);
        double rate2 =  (height * 1.0) / (sourceHeight * 1.0);
        
        double rate = rate1 > rate2 ? rate2 : rate1;
        
        int realWidth = (int) Math.ceil(sourceWidth * rate);
        int realHeight = (int) Math.ceil(sourceHeight * rate);
        try {
            inStream = new FileInputStream(sourcePath);
            BufferedImage sourcebuffImg = ImageIO.read(inStream);
            
            BufferedImage buffImg = new BufferedImage((int) realWidth, (int) realHeight, sourcebuffImg.getType());  //BufferedImage.TYPE_INT_RGB
    
            Graphics2D g = buffImg.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, realWidth, realHeight);
            g.drawImage(img, 0, 0, realWidth, realHeight, null);
            g.dispose();
            img.flush();
            
            destStream = new FileOutputStream(destPath);
            JPEGImageEncoder jpgEncoder = JPEGCodec.createJPEGEncoder(destStream);
            JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(buffImg);  
            encodeParam.setQuality(5f, false);
            jpgEncoder.setJPEGEncodeParam(encodeParam);
            
            jpgEncoder.encode(buffImg);
            destStream.close();
            inStream.close();
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        } finally {
            if (null != destStream) {
                destStream.close();
            }
            if (null != inStream) {
                inStream.close();
            }
        }
        
        return new Integer[] {realWidth, realHeight};
    }
}